package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.CorpModel;
	import com.micmac.customer.model.UserModel;
	import com.micmac.customer.view.UserView;
	import com.micmac.customer.vo.UserRoleVO;
	import com.micmac.customer.vo.UserTitleVO;
	import com.micmac.customer.vo.UserVO;
	
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.profiler.showRedrawRegions;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;

	public class UserViewMediator extends ModuleMediator
	{
		[Inject]
		public var userView:UserView;
		[Inject]
		public var customerService:ICustomerService;
		[Inject]
		public var _dataModel:UserModel;
		[Bindable]
		private var _titleIndex:int = 0;
		private var _userIndex:int = 0;
		private var _userRoleIndex:int = 0;
		private var _fileRef:FileReference = new FileReference();
		private var _byteArray:ByteArray = new ByteArray;
		[Bindable]
		private var _currentStackIndex:int = 0;
		
		private static const LIST_VIEW_INDEX:int = 4;
		private static const TILE_VIEW_INDEX:int = 3;
		
		override public function onRegister():void
		{
			addViewListener(UserView.SAVE_USER_TITLE,onSaveUserTitle);
			addViewListener(UserView.PREVIOUS_USER_TITLE,onPreviousUserTitle);
			addViewListener(UserView.NEXT_USER_TITLE,onNextUserTitle);
			
			addViewListener(UserView.SAVE_USER,onSaveUser);
			addViewListener(UserView.PREVIOUS_USER,onPreviousUser);
			addViewListener(UserView.NEXT_USER,onNextUser);
			
			addViewListener(UserView.SAVE_ROLE,onSaveUserRole);
			addViewListener(UserView.PREVIOUS_USER_ROLE,onPreviousUserRole);
			addViewListener(UserView.NEXT_USER_ROLE,onNextUserRole);
			
			addViewListener(UserView.UPLOAD_CLICKED,onBrowse);
			addViewListener(UserView.STACK_CHANGED,onStackChanged);
			
			addViewListener(UserView.ADD_USER,onAddUser);
			
			addViewListener(UserView.SHOW_LIST_VIEW,onShowListView);
			addViewListener(UserView.SHOW_TILE_VIEW,onShowTileView);
			
			addViewListener(UserView.ON_BACK,onBack);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.USER_TITLE_SAVED,onUserTitleSaved);
			eventMap.mapListener(eventDispatcher,CustomerEvent.USER_SAVED,onUserSaved);
			eventMap.mapListener(eventDispatcher,CustomerEvent.USER_ROLE_SAVED,onUserRoleSaved);
		}
		
		private function onSaveUserTitle(event:Event):void
		{
			var userTitleVo:UserTitleVO = new UserTitleVO();
			userTitleVo.firstName = userView.userTitleView.firstNameText.text;
			userTitleVo.lastName = userView.userTitleView.lastNameText.text;
			userTitleVo.middleName = userView.userTitleView.middlenameText.text;
			userTitleVo.address = userView.userTitleView.addressText.text;
			userTitleVo.city = userView.userTitleView.cityText.text;
			userTitleVo.state = userView.userTitleView.stateText.text;
			userTitleVo.zip = userView.userTitleView.zipText.text;
			customerService.saveUserTitle(userTitleVo);
		}
		
		private function onSaveUser(event:Event):void
		{
			var userVo:UserVO = new UserVO();
			userVo.email = userView.userFormView.emailText.text;
			userVo.phone = userView.userFormView.phoneText.text;
			userVo.mobile = userView.userFormView.mobileText.text;
			userVo.startDate = userView.userFormView.startDateField.text;
			userVo.endDate = userView.userFormView.endDateField.text;
			if(userView.userFormView.activeCheck.selected == true)
			{
				userVo.isActive = true;
			}else
			{
				userVo.isActive = false;
			}
			userVo.description = userView.userFormView.descText.text;
			userVo.userType = userView.userFormView.userTypeCombo.textInput.text;
			userVo.role = userView.userFormView.roleCombo.textInput.text;
			userVo.organasation = userView.userFormView.orgCombo.textInput.text;
			userVo.manager = userView.userFormView.managerCombo.textInput.text;
			userVo.userImage = this._byteArray;
			customerService.saveUser(userVo);
		}
		
		private function onSaveUserRole(event:Event):void
		{
			var userRoleVO:UserRoleVO = new UserRoleVO();
			userRoleVO.name = userView.userRoleView.userRoleNameText.text;
			userRoleVO.description = userView.userRoleView.userRoleDescText.text;
			userRoleVO.discount = userView.userRoleView.userRoleNumericStepper.textDisplay.text;
			if(userView.userRoleView.userRoleDirectCheck.selected == true)
			{
				userRoleVO.isDirectReport = true;
			}else
			{
				userRoleVO.isDirectReport = false;
			}
			customerService.saveUserRole(userRoleVO);
		}
		
		private function onUserTitleSaved(e:CustomerEvent = null):void
		{
			if(_dataModel.userTitleList.length != 1 && _dataModel.userTitleList.length != 0)
			{
				userView.prevBtn.enabled = true;
			}else
			{
				userView.prevBtn.enabled = false;
			}
			userView.addTitleBtn.enabled = true;
			userView.saveBtn.enabled = false;
			userView.nextBtn.enabled = false;
			_titleIndex = _dataModel.userTitleList.length - 1;
			userView.addTitleBtn.label = '+title';
			
			if(_dataModel.userTitleList.length!=0)
			{
				userView.listBtn.enabled = true;
				userView.tileBtn.enabled = true;
			}else
			{
				userView.listBtn.enabled = false;
				userView.tileBtn.enabled = false;
			}
			
		}
		
		private function onUserSaved(event:Event = null):void
		{
			if(_dataModel.userList.length != 1 && _dataModel.userList.length != 0)
			{
				userView.prevBtn.enabled = true;
			}else
			{
				userView.prevBtn.enabled = false;
			}
			userView.addTitleBtn.enabled = true;
			userView.saveBtn.enabled = false;
			userView.nextBtn.enabled = false;
			_userIndex = _dataModel.userList.length - 1;
			userView.addTitleBtn.label = '+user';
			
			if(_dataModel.userList.length!=0)
			{
				userView.listBtn.enabled = true;
				userView.tileBtn.enabled = true;
			}else
			{
				userView.listBtn.enabled = false;
				userView.tileBtn.enabled = false;
			}
		}
		
		private function onUserRoleSaved(event:Event = null):void
		{
			if(_dataModel.userRoleList.length != 1 && _dataModel.userRoleList.length != 0)
			{
				userView.prevBtn.enabled = true;
			}else
			{
				userView.prevBtn.enabled = false;
			}
			userView.addTitleBtn.enabled = true;
			userView.nextBtn.enabled = false;
			userView.saveBtn.enabled = false;
			_userRoleIndex = _dataModel.userRoleList.length - 1;
			userView.addTitleBtn.label = '+role';
			
			if(_dataModel.userRoleList.length!=0)
			{
				userView.listBtn.enabled = true;
				userView.tileBtn.enabled = true;
			}else
			{
				userView.listBtn.enabled = false;
				userView.tileBtn.enabled = false;
			}
		}
		
		private function onPreviousUserTitle(event:Event):void
		{
			_titleIndex--;
			userView.nextBtn.enabled = true;
			userView.saveBtn.enabled = false;
			
			userView.userTitleView.firstNameText.text = _dataModel.userTitleList[_titleIndex].firstName;
			userView.userTitleView.lastNameText.text = 	_dataModel.userTitleList[_titleIndex].lastName;
			userView.userTitleView.middlenameText.text = _dataModel.userTitleList[_titleIndex].middleName;
			userView.userTitleView.addressText.text = _dataModel.userTitleList[_titleIndex].address;
			userView.userTitleView.cityText.text = _dataModel.userTitleList[_titleIndex].city;
			userView.userTitleView.stateText.text = _dataModel.userTitleList[_titleIndex].state;
			userView.userTitleView.cityText.text = _dataModel.userTitleList[_titleIndex].zip;
			
			
			if(_titleIndex == 0)
			{
				userView.prevBtn.enabled = false;
			}
		}
		
		private function onNextUserTitle(event:Event):void
		{
			_titleIndex++;
			userView.saveBtn.enabled = false;
			userView.prevBtn.enabled = true;
			userView.userTitleView.firstNameText.text = _dataModel.userTitleList[_titleIndex].firstName;
			userView.userTitleView.lastNameText.text = 	_dataModel.userTitleList[_titleIndex].lastName;
			userView.userTitleView.middlenameText.text = _dataModel.userTitleList[_titleIndex].middleName;
			userView.userTitleView.addressText.text = _dataModel.userTitleList[_titleIndex].address;
			userView.userTitleView.cityText.text = _dataModel.userTitleList[_titleIndex].city;
			userView.userTitleView.stateText.text = _dataModel.userTitleList[_titleIndex].state;
			userView.userTitleView.cityText.text = _dataModel.userTitleList[_titleIndex].zip;
			if(_titleIndex == _dataModel.userTitleList.length - 1)
			{
				userView.nextBtn.enabled = false;
			}
		}
		
		private function onPreviousUser(event:Event):void
		{
			_userIndex--;
			userView.nextBtn.enabled = true;
			userView.saveBtn.enabled = false;
			
			userView.userFormView.emailText.text = _dataModel.userList[_userIndex].email;
			userView.userFormView.phoneText.text = _dataModel.userList[_userIndex].phone;
			userView.userFormView.mobileText.text = _dataModel.userList[_userIndex].mobile;
			userView.userFormView.startDateField.text = _dataModel.userList[_userIndex].startDate;
			userView.userFormView.endDateField.text = _dataModel.userList[_userIndex].endDate;
			userView.userFormView.descText.text = _dataModel.userList[_userIndex].description;
			userView.userFormView.userTypeCombo.textInput.text = _dataModel.userList[_userIndex].userType;
			userView.userFormView.roleCombo.textInput.text = _dataModel.userList[_userIndex].role;
			userView.userFormView.orgCombo.textInput.text = _dataModel.userList[_userIndex].organasation;
			userView.userFormView.managerCombo.textInput.text = _dataModel.userList[_userIndex].manager;
			
			if(_dataModel.userList[_userIndex].isActive == true)
			{
				userView.userFormView.activeCheck.selected = true;
			}else
			{
				userView.userFormView.activeCheck.selected = false;
			}
			
			if(_dataModel.userList[_userIndex].userImage.bytesAvailable == 0)
			{
				userView.defaultImageSet();
			}else
			{
				userView.userFormView.imageView.browseImageSource = _dataModel.userList[_userIndex].userImage;
			}
			
			if(_userIndex == 0)
			{
				userView.prevBtn.enabled = false;
			}
		}
		
		
		private function onNextUser(event:Event):void
		{
			_userIndex++;
			userView.prevBtn.enabled = true;
			userView.saveBtn.enabled = false;
			
			userView.userFormView.emailText.text = _dataModel.userList[_userIndex].email;
			userView.userFormView.phoneText.text = _dataModel.userList[_userIndex].phone;
			userView.userFormView.mobileText.text = _dataModel.userList[_userIndex].mobile;
			userView.userFormView.startDateField.text = _dataModel.userList[_userIndex].startDate;
			userView.userFormView.endDateField.text = _dataModel.userList[_userIndex].endDate;
			userView.userFormView.descText.text = _dataModel.userList[_userIndex].description;
			userView.userFormView.userTypeCombo.textInput.text = _dataModel.userList[_userIndex].userType;
			userView.userFormView.roleCombo.textInput.text = _dataModel.userList[_userIndex].role;
			userView.userFormView.orgCombo.textInput.text = _dataModel.userList[_userIndex].organasation;
			userView.userFormView.managerCombo.textInput.text = _dataModel.userList[_userIndex].manager;
			
			if(_dataModel.userList[_userIndex].isActive == true)
			{
				userView.userFormView.activeCheck.selected = true;
			}else
			{
				userView.userFormView.activeCheck.selected = false;
			}
			if(_dataModel.userList[_userIndex].userImage.bytesAvailable == 0)
			{
				userView.defaultImageSet();
			}else
			{
				userView.userFormView.imageView.browseImageSource = _dataModel.userList[_userIndex].userImage;
			}
			
			if(_userIndex == _dataModel.userList.length - 1)
			{
				userView.nextBtn.enabled = false;
			}
		}
		
		private function onPreviousUserRole(event:Event):void
		{
			_userRoleIndex--;
			userView.nextBtn.enabled = true;
			userView.saveBtn.enabled = false;
			userView.userRoleView.userRoleNameText.text = _dataModel.userRoleList[_userRoleIndex].name;
			userView.userRoleView.userRoleDescText.text = _dataModel.userRoleList[_userRoleIndex].description; 
			userView.userRoleView.userRoleNumericStepper.textDisplay.text = _dataModel.userRoleList[_userRoleIndex].discount;
			if(_dataModel.userRoleList[_userRoleIndex].isDirectReport == true)
			{
				userView.userRoleView.userRoleDirectCheck.selected = true;
			}else
			{
				userView.userRoleView.userRoleDirectCheck.selected = false;
			}
			if(_userRoleIndex == 0)
			{
				userView.prevBtn.enabled = false;
			}
			
		}
		
		private function onNextUserRole(event:Event):void
		{
			_userRoleIndex++;
			
			userView.userRoleView.userRoleNameText.text = _dataModel.userRoleList[_userRoleIndex].name;
			userView.userRoleView.userRoleDescText.text = _dataModel.userRoleList[_userRoleIndex].description; 
			userView.userRoleView.userRoleNumericStepper.textDisplay.text = _dataModel.userRoleList[_userRoleIndex].discount;
			if(_dataModel.userRoleList[_userRoleIndex].isDirectReport == true)
			{
				userView.userRoleView.userRoleDirectCheck.selected = true;
			}else
			{
				userView.userRoleView.userRoleDirectCheck.selected = false;
			}
			if(_userRoleIndex == _dataModel.userRoleList.length - 1)
			{
				userView.nextBtn.enabled = false;
			}
			
		}
		
		private function onStackChanged(event:Event):void
		{
			switch(userView.vsUser.selectedIndex)
			{
				case 0:
					onUserTitleSaved();
					userView.saveBtn.enabled = true;
					userView.addTitleBtn.enabled = true;
				break;
				case 1:
					onUserRoleSaved();
					userView.saveBtn.enabled = true;
					userView.addTitleBtn.enabled = true;
				break;
				case 2:
					onUserSaved()
					userView.saveBtn.enabled = true;
					userView.addTitleBtn.enabled = true;
				break;
			}
		}
		
		private function onAddUser(event:Event):void
		{
			this._byteArray = new ByteArray;
		}
		
		private function onBrowse(event:Event):void
		{
			_fileRef.browse();
			_fileRef.addEventListener(Event.SELECT, onFileSelect);
		}
		private function onFileSelect(event:Event):void
		{
			_fileRef.addEventListener(Event.COMPLETE, onLoadCmplete);
			_fileRef.load();	
		}
		
		private function onLoadCmplete(event:Event):void
		{
			_fileRef.removeEventListener(Event.SELECT, onFileSelect);
			_fileRef.removeEventListener(Event.COMPLETE, onLoadCmplete);
			_byteArray = new ByteArray();
			_byteArray = _fileRef.data;
			userView.userFormView.imageView.browseImageSource = _byteArray;
			//var sendVars:URLVariables = new URLVariables();
			//sendVars.action = "upload";
			//sendVars.userID = _dataModel.domainerVO.user_id;
			//var request:URLRequest = new URLRequest();
			//	request.data = sendVars;
			//request.url = GlobalConsts.FILE_UPLOAD_URL;
			//request.method = URLRequestMethod.POST;
			
			//_fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onCompleteData);
			//_fileRef.addEventListener(ProgressEvent.PROGRESS,onProgress);
			//_fileRef.addEventListener(IOErrorEvent.IO_ERROR,onError);
			//_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityHandler);
			//_fileRef.upload(request,"file",false);
		}
		
		private function onShowListView(event:Event):void
		{
			switch(userView.vsUser.selectedIndex)
			{
				case 0:
					userView.currentState = 'list';
					userView.userTitleView.userTitleList = _dataModel.userTitleList;
					userView.userTitleView.userTitlevs.selectedIndex = 2;
					_currentStackIndex = 0;
					break;
				case 1:
					userView.currentState = 'list';
					userView.userRoleView.userRoleList = _dataModel.userRoleList;
					userView.userRoleView.userRolevs.selectedIndex = 2;
					_currentStackIndex = 1;
					break;
				case 2:
					userView.currentState = 'list';
					userView.userFormView.userList = _dataModel.userList;
					userView.userFormView.userFormVs.selectedIndex = 2;
					_currentStackIndex = 2;
					break;
			}
		}
		
		private function onShowTileView(event:Event):void
		{
			switch(userView.vsUser.selectedIndex)
			{
				case 0:
					userView.currentState = 'tile';
					userView.userTitleView.userTitleList = _dataModel.userTitleList;
					userView.userTitleView.userTitlevs.selectedIndex = 1;
					_currentStackIndex = 0;
					break;
				case 1:
					userView.currentState = 'tile';
					userView.userRoleView.userRoleList = _dataModel.userRoleList;
					userView.userRoleView.userRolevs.selectedIndex = 1;
					_currentStackIndex = 1;
					break;
				case 2:
					userView.currentState = 'tile';
					userView.userFormView.userList = _dataModel.userList;
					userView.userFormView.userFormVs.selectedIndex = 1;
					_currentStackIndex = 2;
					break;
			}
		}
		
		private function onBack(event:Event):void
		{
			switch(userView.vsUser.selectedIndex)
			{
				case 0:
					userView.userTitleView.userTitlevs.selectedIndex = 0;
					break;
				case 1:
					userView.userRoleView.userRolevs.selectedIndex = 0;
					break;
				case 2:
					userView.userFormView.userFormVs.selectedIndex = 0;
					break;
				
			}
			userView.currentState = 'normal';
		}
	}
}